package lanbo.cj.lmp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import com.baidu.location.LocationClient;
import lanbo.cj.lmp.BaseApplication;
import lanbo.cj.lmp.R;
import lanbo.cj.lmp.bean.Record;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HistoryActivity extends Activity {
    private ListView historyList;
    private ImageView mBack;
    private String ASSETNAME = "assetName", KESHI = "keshiName", UPDATEDAT = "updateAt";
    public BaseApplication mApplication;
    private LocationClient mClient;
    private String lat, lon;
    private ArrayList<Map<String, String>> hisData;
    private Map<String, String> hisItems;
    private String assetId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.history);
        mApplication = (BaseApplication) getApplication();
        mClient = mApplication.mLocationClient;
        if (!mClient.isStarted()) mClient.start();
        DecimalFormat df = new DecimalFormat("#.00");
        lat = df.format(mApplication.mLatitude);
        lon = df.format(mApplication.mLongitude);
        mClient.requestLocation();
        if (getIntent().getStringExtra("assetId") != null) {
            assetId = getIntent().getStringExtra("assetId");
        } else {
            assetId = "111";//若无就搜索结果为0
        }
        findbyid();
        setListener();
    }

    private void findbyid() {
        mBack = (ImageView) findViewById(R.id.history_back);
        historyList = (ListView) findViewById(R.id.history_items);

        getHisData();
    }

    private void setListener() {
        mBack.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 获得记录
     * */
    private void getHisData() {

        hisData = new ArrayList<Map<String, String>>();
        BmobQuery<Record> query = new BmobQuery<Record>();

        //当从标签记录节目跳转到该界面时，查询assertId，从用户操作记录界面跳转时，查询custId
        if (assetId.equals("user")) {
            query.addWhereEqualTo("custId", BmobUser.getCurrentUser(HistoryActivity.this).getObjectId());
        } else {
            query.addWhereEqualTo("assertId", assetId);
        }
        query.findObjects(HistoryActivity.this, new FindListener<Record>() {

            @Override
            public void onError(int arg0, String arg1) {
                // TODO Auto-generated method stub
                Toast.makeText(HistoryActivity.this, "查询历史失败" + arg1, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(List<Record> records) {
                // TODO Auto-generated method stub
                for (Record record : records) {
                    hisItems = (Map<String, String>) new HashMap<String, String>();
                    hisItems.put(ASSETNAME, record.getOperationType());
                    hisItems.put(KESHI, "科室:"+record.getKeshiName());
                    hisItems.put(UPDATEDAT, "借用时间："+record.getCreatedAt());
                    hisData.add(hisItems);

                }
                SimpleAdapter hisSimpleAdapter = new SimpleAdapter(HistoryActivity.this, hisData, R.layout.history_list, new String[]{ASSETNAME, KESHI, UPDATEDAT}, new int[]{R.id.his_assetName, R.id.his_keshi, R.id.his_endTime});
                historyList.setAdapter(hisSimpleAdapter);
            }
        });

    }

    // @Override
    //protected void onDestroy() {
    //      this.unregisterReceiver(netChangeBroad);
    //}

}
